package com.first.member.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.TbMember;
import com.first.common.util.VerificationUtil;
import com.first.common.vo.Result;
import com.first.member.mapper.MemberMapper;
import com.first.member.service.MemberService;
import com.first.member.vo.MemberVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @program: first-project
 * @description:
 * @author: 马英贤
 * @create: 2023-05-30 14:54
 **/
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;
    @Override
    public Page<TbMember> findByPage(Integer current, Integer pageSize, MemberVo memberVo) {
        Page<TbMember> page = new Page<>(current,pageSize);
        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(memberVo.getMobile())){
            wrapper.like("mobile",memberVo.getMobile());
        }if (StringUtils.hasText(memberVo.getNickname())){
            wrapper.like("nickname",memberVo.getNickname());
        }if (memberVo.getGmtCreate()!=null&&memberVo.getGmtCreate().size()<=2){
         //   System.out.println("_-----------"+memberVo.getGmtCreate().get(0));
         //   System.out.println("_-----------"+memberVo.getGmtCreate().get(1));
            wrapper.between("gmt_create",memberVo.getGmtCreate().get(0),memberVo.getGmtCreate().get(1));
        }
        if (memberVo.getIsDisabled()!=null){
            wrapper.eq("is_disabled",memberVo.getIsDisabled());
        }
        Page<TbMember> page1 = memberMapper.selectPage(page,wrapper);
        //System.out.println("-------"+page1);
        return page1;
    }

    @Override
    public Integer updateIsDisabled(String id, Integer isDisabled) {
        TbMember member = new TbMember();
        member.setId(id);
        member.setIsDisabled(isDisabled);
        member.setGmtModified(LocalDateTime.now());
        Integer update = memberMapper.updateById(member);
        return update;
    }

    @Override
    public void update(TbMember tbMember) {
        memberMapper.updateById(tbMember);
    }

    @Override
    public Result getYanZhengMa(String mobile) {
        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        TbMember tbMember = memberMapper.selectOne(wrapper);
        if(tbMember!=null){
            return Result.fail("手机号重复，注册失败");
        }
        String code = VerificationUtil.getCode(mobile);
        return Result.success(code);
    }

    @Override
    public Result installMember(String mobile, String qianPassword) {
        TbMember tbMember = new TbMember();
        tbMember.setPassword(DigestUtil.md5Hex(qianPassword));
        tbMember.setMobile(mobile);
        tbMember.setId(IdUtil.getSnowflakeNextId()+"");
        tbMember.setGmtModified(LocalDateTime.now());
        tbMember.setGmtCreate(LocalDateTime.now());
        int insert = memberMapper.insert(tbMember);
        if(insert!=0){
            return Result.success();
        }
        return Result.fail();
    }

    /**
     *
     * @param mobile
     * @return 查询个人信息
     */
    @Override
    public TbMember selectOne(String mobile) {
        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        return memberMapper.selectOne(wrapper);
    }

    /**
     *
     * @param tbMember
     * @return 修改个人信息
     */
    @Override
    public Integer updateUser(TbMember tbMember) {
        int update = memberMapper.updateById(tbMember);
        return update;
    }

    /**
     *
     * @param mobile
     * @param newPassword
     * @return 修改密码
     */
    @Override
    public Integer updateMemberPassword(String mobile, String newPassword) {
        QueryWrapper<TbMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        TbMember tbMember=new TbMember();
        tbMember.setPassword(DigestUtil.md5Hex(newPassword));
        Integer integer = memberMapper.update(tbMember,wrapper);
        return integer;
    }

    @Override
    public Result getYanZhengMa1(String mobile) {
        String code = VerificationUtil.getCode(mobile);
        return Result.success(code);
    }


}
